package templateController

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"log"
	"os"
)

const (
	contextPath = "./views/"
	list_1      = "./static/res/template/list_1.tmpl"
)

func Router(router *gin.Engine) {
	router.GET("/template/generatePage", generatePage)
}



func generatePage(context *gin.Context) {
	var (
		err error
	)
	module := "sys"
	model := "role"
	content := "角色页面"
	err = os.MkdirAll(contextPath+module+"/"+model, 0777)
	if err != nil {
		fmt.Printf("%s", err)
	} else {
		bs, err := ioutil.ReadFile(list_1)
		if err != nil {
			log.Printf("模块[%s]模型:[%s],读取模板[%s]失败", module, model, "list_1.tmpl")
		} else {
			log.Println("开始生成列表页面......")
			f1, err := os.OpenFile(contextPath+module+"/"+model+"/list.html", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0777)
			fmt.Println(err)
			defer f1.Close()
			n, err := fmt.Fprintf(f1, string(bs), module+"/"+model+".html", "标题", content)
			if err != nil {
				log.Println("生成列表页面失败:", err)
			} else {
				log.Println("生成列表页面成功:", n)
			}
		}
	}
}
